package com.gitee.wsl.result.tran


import com.gitee.wsl.result.sample.ValueResult
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract


@OptIn(ExperimentalContracts::class)
inline fun <R, T,E> ValueResult<T, E>.fold(
    onSuccess: (value: T) -> R,
    onFailure: (failure: E) -> R,
): R {
    contract {
        callsInPlace(onSuccess, kotlin.contracts.InvocationKind.AT_MOST_ONCE)
        callsInPlace(onFailure, kotlin.contracts.InvocationKind.AT_MOST_ONCE)
    }
    return when {
        isOk -> onSuccess(this.value)
        isErr -> onFailure(error)
        //else -> onFailure(Throwable("Unhandled DataResult"))
        else -> TODO()
    }
}
